class Customer extends egret.Sprite {
  // 等位定时器flag
  private _waitSeatTimer?: number;
  // 等位时长
  private _waitSeatTime = 20000;
  /**
   * 进入等位状态
   */
  waitingSeat() {
    if (this._waitSeatTimer) return;
    this._waitSeatTimer = egret.setTimeout(() => {
      this.leave();
      this._waitSeatTimer = undefined;
    }, this, this._waitSeatTime);
  }
  /**
   * 清除等位状态
   */
  cleanWaitingSeat() {
    if (this._waitSeatTimer) egret.clearTimeout(this._waitSeatTimer);
  }
  private _speed = 100;
  private _process = new ProcessBar();
  private _observer = Observer.getInstance();
  private _waitTimer: number | null = null;
  private _bitmap: egret.Bitmap;
  waitTime: number = 10 * 1000;
  constructor(private _food: Food, texture: string) {
    super();
    this._bitmap = new egret.Bitmap();
    this._bitmap.texture = RES.getRes(texture);
    this._bitmap.width = 64;
    this._bitmap.height = 64;
    this.width = 64;
    this.height = 64;
    this._process.y = -10;
    this.addChild(this._bitmap);
  }
  async leave() {
    this._observer.emit(GameEvent.CUSTOMER_LEAVED, this);
    await this.moveTo(0, 600);
    this.onLeaveOver();
  }

  moveTo(x: number, y: number) {
    return new Promise((resolve) => {
      const distance = Utils.pointDistance({ x: this.x, y: this.y }, { x, y });
      egret.Tween.removeTweens(this);
      egret.Tween.get(this)
        .to({ x, y }, (distance / this._speed) * 1000)
        .call(resolve, null);
    });
  }
  getFood() {
    return this._food;
  }
  private onLeaveOver() {
    egret.Tween.removeTweens(this);
    this.parent?.removeChild(this);
  }
  async waitingFood() {
    try {
      await this._showWantFood();
      return "success";
    } catch (error) {
      return "timeout";
    }
  }

  private _showWantFood() {
    return new Promise((resolve, reject) => {
      this._food.y = -70;
      this._food.touchEnabled = true;
      this.addChild(this._food);
      const clean = () => {
        if (this._waitTimer) egret.clearTimeout(this._waitTimer);
        this._food.removeEventListener(
          egret.TouchEvent.TOUCH_TAP,
          callback,
          this
        );
        this._food.touchEnabled = false;
        this.removeChild(this._food);
      };
      const callback = () => {
        clean();
        resolve(undefined);
      };
      const timeout = () => {
        this.waitTimeout();
        clean();
        reject();
      };
      this._waitTimer = egret.setTimeout(timeout, this, this.waitTime);
      this._food.addEventListener(egret.TouchEvent.TOUCH_TAP, callback, this);
    });
  }

  private waitTimeout() {
    if (this._waitTimer) egret.clearTimeout(this._waitTimer);
    this.leave();
  }

  eat() {
    return new Promise((resolve) => {
      egret.Tween.removeTweens(this);
      this.addChild(this._process);
      egret.Tween.get(this._process)
        .to({ process: 1 }, 1000)
        .call(() => {
          this.removeChild(this._process);
          egret.Tween.removeTweens(this._process);
          resolve(undefined);
        }, this);
    });
  }
}

interface BusinessLog {
  food: Food;
  count: number;
}

class ProcessBar extends egret.Sprite {
  private _width = 60;
  private _height = 4;
  private _process = 0;

  set process(value: number) {
    this.graphics.beginFill(0xffffff);
    this.graphics.drawRect(0, 0, this._width, this._height);
    this.graphics.beginFill(0x00ff00);
    this.graphics.drawRect(0, 0, this._width * value, this._height);
    this.graphics.endFill();
  }

  get process() {
    return this._process;
  }
}
